package com.ruoyi.reportsnhnt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntBhwmslBaDetail;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntBhwmslBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportsnhnt.domain.bo.ReportSnhntBhwmslBsInfoBo;
import com.ruoyi.reportsnhnt.domain.vo.ReportSnhntBhwmslBsInfoVo;
import com.ruoyi.reportsnhnt.domain.ReportSnhntBhwmslBsInfo;
import com.ruoyi.reportsnhnt.mapper.ReportSnhntBhwmslBsInfoMapper;
import com.ruoyi.reportsnhnt.service.IReportSnhntBhwmslBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 水泥混凝土拌合物泌水率试验检测记录Service业务层处理
 *
 * @author zxs
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class ReportSnhntBhwmslBsInfoServiceImpl implements IReportSnhntBhwmslBsInfoService {

    private final ReportSnhntBhwmslBsInfoMapper baseMapper;
    private final ReportSnhntBhwmslBaDetailMapper detailMapper;

    /**
     * 查询水泥混凝土拌合物泌水率试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportSnhntBhwmslBsInfoVo queryById(Long excelId){
        ReportSnhntBhwmslBsInfoVo vo = baseMapper.selectVoById(excelId);
        LambdaQueryWrapper<ReportSnhntBhwmslBaDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ReportSnhntBhwmslBaDetail::getExcelId,excelId);
        List<ReportSnhntBhwmslBaDetail> detailList = detailMapper.selectList(wrapper);
        if (CollUtil.isNotEmpty(detailList)) {
            vo.setDetailList(detailList);
        }
        return vo;
    }

    /**
     * 查询水泥混凝土拌合物泌水率试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportSnhntBhwmslBsInfoVo> queryPageList(ReportSnhntBhwmslBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSnhntBhwmslBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportSnhntBhwmslBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询水泥混凝土拌合物泌水率试验检测记录列表
     */
    @Override
    public List<ReportSnhntBhwmslBsInfoVo> queryList(ReportSnhntBhwmslBsInfoBo bo) {
        LambdaQueryWrapper<ReportSnhntBhwmslBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSnhntBhwmslBsInfo> buildQueryWrapper(ReportSnhntBhwmslBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSnhntBhwmslBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPage() != null, ReportSnhntBhwmslBsInfo::getPage, bo.getPage());
        lqw.eq(bo.getTotalPage() != null, ReportSnhntBhwmslBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportSnhntBhwmslBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportSnhntBhwmslBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportSnhntBhwmslBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportSnhntBhwmslBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpxx()), ReportSnhntBhwmslBsInfo::getYangpxx, bo.getYangpxx());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportSnhntBhwmslBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportSnhntBhwmslBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportSnhntBhwmslBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportSnhntBhwmslBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(bo.getLaiysj() != null, ReportSnhntBhwmslBsInfo::getLaiysj, bo.getLaiysj());
        lqw.eq(bo.getShiyjcrq() != null, ReportSnhntBhwmslBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytj()), ReportSnhntBhwmslBsInfo::getShiytj, bo.getShiytj());
        lqw.eq(StringUtils.isNotBlank(bo.getWend()), ReportSnhntBhwmslBsInfo::getWend, bo.getWend());
        lqw.eq(StringUtils.isNotBlank(bo.getXiangdsd()), ReportSnhntBhwmslBsInfo::getXiangdsd, bo.getXiangdsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportSnhntBhwmslBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportSnhntBhwmslBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportSnhntBhwmslBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportSnhntBhwmslBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportSnhntBhwmslBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportSnhntBhwmslBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportSnhntBhwmslBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportSnhntBhwmslBsInfo::getFuh, bo.getFuh());
        lqw.eq(bo.getRiq() != null, ReportSnhntBhwmslBsInfo::getRiq, bo.getRiq());
        lqw.eq(bo.getKaissyjssj() != null, ReportSnhntBhwmslBsInfo::getKaissyjssj, bo.getKaissyjssj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiaobfs()), ReportSnhntBhwmslBsInfo::getJiaobfs, bo.getJiaobfs());
        lqw.eq(bo.getXisljzl1() != null, ReportSnhntBhwmslBsInfo::getXisljzl1, bo.getXisljzl1());
        lqw.eq(bo.getXisljzl2() != null, ReportSnhntBhwmslBsInfo::getXisljzl2, bo.getXisljzl2());
        lqw.eq(bo.getXisljzl3() != null, ReportSnhntBhwmslBsInfo::getXisljzl3, bo.getXisljzl3());
        lqw.eq(bo.getShijwlbmmj() != null, ReportSnhntBhwmslBsInfo::getShijwlbmmj, bo.getShijwlbmmj());
        lqw.eq(bo.getMisl1() != null, ReportSnhntBhwmslBsInfo::getMisl1, bo.getMisl1());
        lqw.eq(bo.getMisl2() != null, ReportSnhntBhwmslBsInfo::getMisl2, bo.getMisl2());
        lqw.eq(bo.getMisl3() != null, ReportSnhntBhwmslBsInfo::getMisl3, bo.getMisl3());
        lqw.eq(bo.getMislpj() != null, ReportSnhntBhwmslBsInfo::getMislpj, bo.getMislpj());
        lqw.eq(bo.getShiytzl1() != null, ReportSnhntBhwmslBsInfo::getShiytzl1, bo.getShiytzl1());
        lqw.eq(bo.getShiytzl2() != null, ReportSnhntBhwmslBsInfo::getShiytzl2, bo.getShiytzl2());
        lqw.eq(bo.getShiytzl3() != null, ReportSnhntBhwmslBsInfo::getShiytzl3, bo.getShiytzl3());
        lqw.eq(bo.getMisqsytjsy1() != null, ReportSnhntBhwmslBsInfo::getMisqsytjsy1, bo.getMisqsytjsy1());
        lqw.eq(bo.getMisqsytjsy2() != null, ReportSnhntBhwmslBsInfo::getMisqsytjsy2, bo.getMisqsytjsy2());
        lqw.eq(bo.getMisqsytjsy3() != null, ReportSnhntBhwmslBsInfo::getMisqsytjsy3, bo.getMisqsytjsy3());
        lqw.eq(bo.getBanhwzzl() != null, ReportSnhntBhwmslBsInfo::getBanhwzzl, bo.getBanhwzzl());
        lqw.eq(bo.getBanhwsxzysl() != null, ReportSnhntBhwmslBsInfo::getBanhwsxzysl, bo.getBanhwsxzysl());
        lqw.eq(bo.getMislv1() != null, ReportSnhntBhwmslBsInfo::getMislv1, bo.getMislv1());
        lqw.eq(bo.getMislv2() != null, ReportSnhntBhwmslBsInfo::getMislv2, bo.getMislv2());
        lqw.eq(bo.getMislv3() != null, ReportSnhntBhwmslBsInfo::getMislv3, bo.getMislv3());
        lqw.eq(bo.getMislvpj() != null, ReportSnhntBhwmslBsInfo::getMislvpj, bo.getMislvpj());
        lqw.eq(StringUtils.isNotBlank(bo.getBeiz()), ReportSnhntBhwmslBsInfo::getBeiz, bo.getBeiz());
        return lqw;
    }

    /**
     * 新增水泥混凝土拌合物泌水率试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportSnhntBhwmslBsInfoBo bo) {
        ReportSnhntBhwmslBsInfo add = BeanUtil.toBean(bo, ReportSnhntBhwmslBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            //插入子表数据
            List<ReportSnhntBhwmslBaDetail> detailList = new ArrayList<>();
            for (int i = 0; i < 12; i++) {
                ReportSnhntBhwmslBaDetail detail = new ReportSnhntBhwmslBaDetail();
                detail.setExcelId(bo.getExcelId());
                detailList.add(detail);
            }
            detailMapper.insertBatch(detailList);
        }
        return flag;
    }

    /**
     * 修改水泥混凝土拌合物泌水率试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(ReportSnhntBhwmslBsInfoBo bo) {
        ReportSnhntBhwmslBsInfo update = BeanUtil.toBean(bo, ReportSnhntBhwmslBsInfo.class);
        validEntityBeforeSave(update);
        //修改子表数据
        List<ReportSnhntBhwmslBaDetail> detailList = bo.getDetailList();
        if(detailList!=null&&detailList.size()!=0){
            detailMapper.updateBatchById(detailList);
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSnhntBhwmslBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除水泥混凝土拌合物泌水率试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
